using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Perovich.GameObjects.ThreeDimensional
{

    /// <summary>
    /// The <see cref="WireframeModelObject3D"/> is a three dimensional object that draws its <see cref="Model"/> as a Wireframe.
    /// </summary>
    public class WireframeModelObject3D : ModelObject3Dbase
    {
        static RasterizerState SolidRaster = new RasterizerState();
        static RasterizerState WireRaster = new RasterizerState();

        /// <summary>
        /// Constructs a new instance of the <see cref="WireframeModelObject3D"/> class.
        /// </summary>
        /// <param name="game"><see cref="Perovich.GameObjects.Interfaces.ICanHoldComponents"/> instance that this <see cref="WireframeModelObject3D"/> will be constructed for.</param>
        public WireframeModelObject3D(Interfaces.ICanHoldComponents game)
            : base(game)
        {
            SolidRaster.FillMode = FillMode.Solid;
            WireRaster.FillMode = FillMode.WireFrame;
        }

        /// <summary>
        /// Allows the <see cref="WireframeModelObject3D"/> to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of the game's timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            if (ObjectModel != null)
            {
                GraphicsDevice.RasterizerState = WireRaster;

                foreach (Camera c in Cameras)
                {
                    Matrix[] transforms = new Matrix[ObjectModel.Bones.Count];
                    ObjectModel.CopyAbsoluteBoneTransformsTo(transforms);
                    Matrix scaleMatrix = Matrix.CreateScale(Scale);
                    Matrix translateMatrix = Matrix.CreateTranslation(Position);
                    Matrix rotationMatrix = Matrix.CreateRotationY(RotationY) * Matrix.CreateRotationX(RotationX);
                    Matrix worldMatrix = scaleMatrix * rotationMatrix * translateMatrix;

                    foreach (ModelMesh mesh in ObjectModel.Meshes)
                    {
                        foreach (BasicEffect effect in mesh.Effects)
                        {
                            effect.World =
                                worldMatrix * transforms[mesh.ParentBone.Index];
                            effect.View = c.ViewMatrix;
                            effect.Projection = c.ProjectionMatrix;

                        }
                        mesh.Draw();
                    }
                }
                GraphicsDevice.RasterizerState = SolidRaster;
            }
            base.Draw(gameTime);
        }
    }
}
